﻿using System.Diagnostics;
using System.IO;
using System.Linq;
using LEShortcut.Dialogs.Xaml;
using LEShortcut.Models.Views;
using LEShortcut.Models.Views.Pages;
using Microsoft.Win32;

namespace LEShortcut.Helper
{
    /// <summary>
    ///     Store reuse part of some command
    /// </summary>
    public static class CommandHelper
    {
        public static void ParserShortcuts(string[] paths, HomeViewContext context)
        {
            if (paths.Any())
            {
                if (paths.Length > 1)
                {
                    if (!string.IsNullOrWhiteSpace(context.ChoosedProfile))
                    {
                        var dialog = new MultipleCreateConfirmDialog(paths.Length);
                        bool? result = dialog.ShowDialog();
                        if (result != null && (bool) result)
                        {
                            var saveDialog = new SaveFileDialog
                            {
                                Title = "ChooseSaveShortcutLocationTitle".GetI18NString(),
                                FileName = "SaveHere".GetI18NString()
                            };
                            bool? showDialog = saveDialog.ShowDialog();
                            if (showDialog != null && (bool) showDialog)
                            {
                                string saveLoc = Path.GetDirectoryName(saveDialog.FileName);
                                int i = 0;
                                foreach (string path in paths)
                                {
                                    var info = new ShellLinkInfo();
                                    if (ShellLinkHelper.Extractor(path, ref info))
                                    {
                                        Debug.Assert(saveLoc != null, "saveLoc != null");
                                        ShellLinkHelper.SaveShortcut(info,
                                            Path.Combine(saveLoc,
                                                string.Concat(Path.GetFileNameWithoutExtension(path), ".lnk")),
                                            Path.Combine(context.Config.LePath, "LEProc.exe"), context.ChoosedProfile);
                                        i++;
                                    }
                                }
                                if (i > 0) new CreateShortcutSucceedDialog(i).ShowDialog();
                                else new NoneSupportedDialog().ShowDialog();
                            }
                        }
                    }
                    else new ChooseProfileDialog().ShowDialog();
                }
                else
                {
                    string p = paths[0];
                    context.FilePath = p;
                    context.FileName = Path.GetFileNameWithoutExtension(p);
                    if (ShellLinkHelper.IsLink(p))
                    {
                        var info = new ShellLinkInfo();
                        if (ShellLinkHelper.Extractor(p, ref info))
                        {
                            context.ExeutionPath = info.ExeutionPath;
                            context.Description = info.Description;
                            context.IconPath = info.IconPath;
                            context.IconIndex = info.IconIndex;
                        }
                        else new UnsupportedShortcutDialog().ShowDialog();
                    }
                    else
                    {
                        context.ExeutionPath = p;
                        context.IconPath = p;
                        context.IconIndex = 0;
                    }
                }
            }
            else new NoValidItemDialog().ShowDialog();
        }
    }
}